import hotReload from '@/utils/hotReload'
import { getStockSuggestList, getStockList } from '../api/api'
import {getChromeLocalStorage, setChromeLocalStorage, showNotification} from '../assets/js/utils'
hotReload()
const backInfo = {
  popopActive: true
};
let active = true

getChromeLocalStorage('popopActive', (popopActive) => {
  active = popopActive || false
})

window.popopActive = active !== 'false';
backInfo.popopActive = active !== 'false';

// 插件激活状态改变
window.popupActiveChange = (active) => {
  window.popopActive = active;
  backInfo.popopActive = active;
  setChromeLocalStorage('popopActive', active)
};

chrome.webRequest.onBeforeSendHeaders.addListener(function (details) {
    const {requestHeaders} = details;
    console.log(details.requestHeaders)
    requestHeaders.push({
      name: 'Referer',
      value: 'http://finance.sina.com.cn/'
    })
    return {requestHeaders};
  }, {
    urls: ['*://*/*']
  },
  ['blocking', 'requestHeaders']
);

chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
  if (request.greeting === 'getBackGroundMessage') {
    chrome.tabs.query({
      active: true,
      currentWindow: true
    }, async function () {
      sendResponse(backInfo);
    });
    return true;
  } else if (request.greeting === 'searchKey') {
    getStockSuggestList(request.data.searchKey).then((res) => {
      sendResponse(res);
    })
  } else if (request.greeting === 'getStockList') {
    getStockList(request.data.codeList).then((res) => {
      sendResponse(res);
    })
  } else if (request.greeting === 'showNotification') {
    const data = request.data
    showNotification(data.title, data.data)
  }
  return true;
})
